package com.cssw.fyzb.busi.api.task;


import com.cssw.fyzb.busi.convert.Converter;
import com.cssw.fyzb.busi.service.team.TeamService;
import com.cssw.fyzb.core.model.vo.task.TaskVo;
import com.cssw.fyzb.busi.service.task.TaskService;
import com.cssw.fyzb.core.model.dodb.Task;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import com.ydn.dbframe.plugin.activerecord.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author
 */
@Function(description = "任务中心", parameters = {
        @Parameter(name = "userId", type = Type.String, description = "用户标识", required = true),
        @Parameter(name = "pageNumber", type = Type.Integer, description = "当前页，从1开始", required = true),
        @Parameter(name = "pageSize", type = Type.Integer, description = "页大小", required = true),
})
@Component
public class ListTask implements Action {

    private final static Logger logger = LoggerFactory.getLogger(ListTask.class);

    @Autowired
    private TaskService taskService;

    @Autowired
    private TeamService teamService;

    @Override
    public Response execute(Request request) throws Exception {
        Long userId = request.getLong("userId");
        Integer pageNumber = request.getInteger("pageNumber");
        Integer pageSize = request.getInteger("pageSize");
        try {
            Page<Task> page = taskService.taskList(userId, pageNumber, pageSize);
            Integer taskTeamNum = teamService.countTaskTeamNum(userId);
            List<TaskVo> list = ((Converter<List<Task>, List<TaskVo>>) videos -> {
                List<TaskVo> vos = new ArrayList<>();
                for (Task v : videos) {
                    TaskVo vo = new TaskVo();
                    vo.setAllValue(v.getAllValue());
                    vo.setCycleDay(v.getCycleDay());
                    vo.setLimitNum(v.getLimitNum());
                    vo.setPrice(v.getPrice());
                    vo.setTaskType(v.getTaskType());
                    vo.setSpecialNote(v.getSpecialNote());
                    vo.setProduceDay(v.getProduceDay());
                    vo.setTaskCond(v.getTaskCond());
                    vo.setTaskId(v.getTaskId());
                    vo.setTaskName(v.getTaskName());
                    vo.setTaskTime(v.getTaskTime());
                    vo.setVitality(v.getVitality());

                    Integer count = taskService.myClaim(userId, v.getTaskId());
                    vo.setMyClaim(count);
                    if (Task.TASK_TYPE_RECOMM_TEAM.equals(v.getTaskType())){
                        if (null != v.getTeamNum() && taskTeamNum >= v.getTeamNum()){
                            if (count == v.getLimitNum()) {
                                vo.setStatus("2");
                            } else {
                                vo.setStatus("1");
                            }
                        } else {
                            vo.setStatus("3");
                        }
                        vo.setTeamNum(v.getTeamNum());
                        vo.setRecommTeamNum(taskTeamNum);
                    }else {
                        if (count == v.getLimitNum()) {
                            vo.setStatus("2");
                        } else {
                            vo.setStatus("1");
                        }
                    }
                    if (v.getTaskId() != 1) {
                        vo.setLncomeRatio(v.getAllValue().divide(v.getPrice(), 7, BigDecimal.ROUND_HALF_DOWN).subtract(new BigDecimal(1)).multiply(new BigDecimal(100)));
                    }else{
                        vo.setLncomeRatio(v.getAllValue().multiply(new BigDecimal(100)));
                    }
                    vo.setTaskIcon(v.getStr("task_icon"));
                    vos.add(vo);
                }
                return vos;
            }).convert(page.getList());
            return Response.success().put("list", list).put("total", page.getTotalPage()).put("count", page.getTotalRow());
        } catch (Exception e) {
            logger.error("", e);
            return Response.fail("查询失败");
        }
    }
}
